package org.jnativehook.example;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.ItemSelectable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.Document;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyException;
import org.jnativehook.keyboard.NativeKeyListener;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseInputListener;

public class NativeHookDemo extends JFrame
  implements NativeKeyListener, NativeMouseInputListener, ActionListener, WindowListener, ItemListener
{
  private static final long serialVersionUID = -5076634313730799059L;
  private JCheckBox chkKeyboard;
  private JCheckBox chkButton;
  private JCheckBox chkMotion;
  private JTextArea txtEventInfo;

  public NativeHookDemo()
  {
    setTitle("JNativeHook Demo");
    setLayout(new BorderLayout());
    setDefaultCloseOperation(3);
    setSize(600, 300);
    addWindowListener(this);
    JPanel localJPanel = new JPanel();
    localJPanel.setBorder(BorderFactory.createEtchedBorder(1));
    localJPanel.setLayout(new FlowLayout(3));
    add(localJPanel, "North");
    this.chkKeyboard = new JCheckBox("Keyboard Events");
    this.chkKeyboard.setMnemonic(75);
    this.chkKeyboard.setSelected(true);
    this.chkKeyboard.addItemListener(this);
    localJPanel.add(this.chkKeyboard);
    this.chkButton = new JCheckBox("Button Events");
    this.chkButton.setMnemonic(66);
    this.chkButton.setSelected(true);
    this.chkButton.addItemListener(this);
    localJPanel.add(this.chkButton);
    this.chkMotion = new JCheckBox("Motion Events");
    this.chkMotion.setMnemonic(77);
    this.chkMotion.setSelected(true);
    this.chkMotion.addItemListener(this);
    localJPanel.add(this.chkMotion);
    this.txtEventInfo = new JTextArea();
    this.txtEventInfo.setEditable(false);
    this.txtEventInfo.setBackground(new Color(255, 255, 255));
    this.txtEventInfo.setForeground(new Color(0, 0, 0));
    this.txtEventInfo.setText("");
    JScrollPane localJScrollPane = new JScrollPane(this.txtEventInfo);
    localJScrollPane.setPreferredSize(new Dimension(375, 125));
    add(localJScrollPane, "Center");
    setVisible(true);
  }

  public void itemStateChanged(ItemEvent paramItemEvent)
  {
    ItemSelectable localItemSelectable = paramItemEvent.getItemSelectable();
    if (localItemSelectable == this.chkKeyboard)
    {
      if (paramItemEvent.getStateChange() == 1)
        GlobalScreen.getInstance().addNativeKeyListener(this);
      else
        GlobalScreen.getInstance().removeNativeKeyListener(this);
    }
    else if (localItemSelectable == this.chkButton)
    {
      if (paramItemEvent.getStateChange() == 1)
        GlobalScreen.getInstance().addNativeMouseListener(this);
      else
        GlobalScreen.getInstance().removeNativeMouseListener(this);
    }
    else if (localItemSelectable == this.chkMotion)
      if (paramItemEvent.getStateChange() == 1)
        GlobalScreen.getInstance().addNativeMouseMotionListener(this);
      else
        GlobalScreen.getInstance().removeNativeMouseMotionListener(this);
  }

  public void actionPerformed(ActionEvent paramActionEvent)
  {
    this.txtEventInfo.setText("");
    requestFocusInWindow();
  }

  public void keyPressed(NativeKeyEvent paramNativeKeyEvent)
  {
    displayEventInfo(paramNativeKeyEvent);
  }

  public void keyReleased(NativeKeyEvent paramNativeKeyEvent)
  {
    displayEventInfo(paramNativeKeyEvent);
  }

  public void mousePressed(NativeMouseEvent paramNativeMouseEvent)
  {
    displayEventInfo(paramNativeMouseEvent);
  }

  public void mouseReleased(NativeMouseEvent paramNativeMouseEvent)
  {
    displayEventInfo(paramNativeMouseEvent);
  }

  public void mouseMoved(NativeMouseEvent paramNativeMouseEvent)
  {
    displayEventInfo(paramNativeMouseEvent);
  }

  private void displayEventInfo(NativeKeyEvent paramNativeKeyEvent)
  {
    this.txtEventInfo.setText(this.txtEventInfo.getText() + "\n" + paramNativeKeyEvent.paramString());
    this.txtEventInfo.setCaretPosition(this.txtEventInfo.getDocument().getLength());
  }

  private void displayEventInfo(NativeMouseEvent paramNativeMouseEvent)
  {
    this.txtEventInfo.setText(this.txtEventInfo.getText() + "\n" + paramNativeMouseEvent.paramString());
    this.txtEventInfo.setCaretPosition(this.txtEventInfo.getDocument().getLength());
  }

  public void windowActivated(WindowEvent paramWindowEvent)
  {
  }

  public void windowClosing(WindowEvent paramWindowEvent)
  {
  }

  public void windowDeactivated(WindowEvent paramWindowEvent)
  {
  }

  public void windowDeiconified(WindowEvent paramWindowEvent)
  {
  }

  public void windowIconified(WindowEvent paramWindowEvent)
  {
  }

  public void windowOpened(WindowEvent paramWindowEvent)
  {
    GlobalScreen.getInstance();
    try
    {
      this.txtEventInfo.setText(this.txtEventInfo.getText() + "\n" + "Auto Repate Rate: " + GlobalScreen.getInstance().getAutoRepeatRate());
      this.txtEventInfo.setText(this.txtEventInfo.getText() + "\n" + "Auto Repate Delay: " + GlobalScreen.getInstance().getAutoRepeatDelay());
    }
    catch (NativeKeyException localNativeKeyException)
    {
      this.txtEventInfo.setText("Error: " + paramWindowEvent.toString() + "\n");
    }
    this.txtEventInfo.setCaretPosition(this.txtEventInfo.getDocument().getLength());
    GlobalScreen.getInstance().addNativeKeyListener(this);
    GlobalScreen.getInstance().addNativeMouseListener(this);
    GlobalScreen.getInstance().addNativeMouseMotionListener(this);
  }

  public void windowClosed(WindowEvent paramWindowEvent)
  {
    System.runFinalization();
    System.exit(0);
  }

  public static void main(String[] paramArrayOfString)
  {
    new NativeHookDemo();
  }
}

/* Location:           D:\Downloads\JNativeHook\jar\JNativeHook.jar
 * Qualified Name:     org.jnativehook.example.NativeHookDemo
 * JD-Core Version:    0.6.0
 */